/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.simpla;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

/**
 *
 * @author Jacob Nordfalk
 */
public abstract class SimplaActivity extends Activity {

    public Simpla simpla = null;
    public boolean simplaMainFinishWillFinishActivity = false;
    public boolean simplaMainFinishNotification = true;

		/** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
				Simpla.init(this);
        simpla = Simpla.getSimpla();
				mainThread.start();
    }

		/** You must define this method and put the simplaMain code for your program here */
		public abstract void simplaMain() throws Exception;

    Thread mainThread = new Thread() {
      @Override
      public void run() {
        try {
          simplaMain();
          if (simplaMainFinishNotification)
            simpla.showToast("simplaMain() has ended: "+SimplaActivity.this.getClass().getSimpleName());
          if (simplaMainFinishWillFinishActivity)
            finish();
        } catch (Exception ex) {
          ex.printStackTrace();
          System.err.println("***********************************************");
          StackTraceElement[] places = ex.getStackTrace();
          boolean first = true;
          String msg = "The program has stopped with a "+ex.getClass().getSimpleName()+": "+ex.getLocalizedMessage();
          for (int i=places.length-2; i>=0; i--) {
            StackTraceElement place = places[i];
            if (first) {
              msg += "\nHave a look in "+place.getFileName()+" line "+place.getLineNumber()+ " (in method "+place.getMethodName()+")";
            } else {
              msg += "\n ... which called "+place.getFileName()+" line "+place.getLineNumber()+ " (method "+place.getMethodName()+")";
            }
            first = false;
          }
          if (ex instanceof NullPointerException) {
            msg += "\nA NullPointerException means that a variable did not refer to an object (was null)";
          } else
          if (ex instanceof ArrayIndexOutOfBoundsException) {
            msg += "\nAn ArrayIndexOutOfBoundsException means that you used an invalid index in an array or ArrayList.";
          } else
          if (ex instanceof NumberFormatException) {
            msg += "\nA NumberFormatException occurs if you try to parse a String as a number, and the String is not a number";
          } else
          if (ex instanceof android.content.ActivityNotFoundException) {
            msg += "\nYou have to edit AndroidManifest.xml to fix this problem";
          }

          System.err.println(msg);
          System.err.println("***********************************************");
          Simpla.getSimpla().showEditTextDialog(ex.toString(), msg);
        }
      }
    };


    @Override
		protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      Simpla.init(this);
			Log.d("Simpla", "onActivityResult("+requestCode+ ", "+resultCode+", "+data);
		}

      protected void onResume() {
				super.onResume();
				Simpla.init(this);
				Log.d("Simpla", "onResume");
				Simpla.notify(this);
			}

      private Object waitForKeySemaphore = new Object();
      private KeyEvent lastKeyEvent;

      public KeyEvent simplaWaitForKeyPress() {
        Simpla.waitForNotify(waitForKeySemaphore);
        return lastKeyEvent;
      }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
      lastKeyEvent = event;
      Simpla.notify(waitForKeySemaphore);
      return super.onKeyDown(keyCode, event);
    }
}
